package cn.edu.scau.cmi.xiaozecheng.singleton;

import java.util.HashSet;
import java.util.Set;

public class Marshal {
	private String name;
	//用HashSet存十大元帅
	private static Set<Marshal> marshals = new HashSet<>();

	public static boolean addInstance(String name) {
		if(marshals.size()>9) {
			return false;
		}
		return marshals.add(new Marshal(name));
	}
	public static Set<Marshal> getMarshals(){
		return marshals;
	}
	/**
	 * 给重来功能添加的方法
	 */
	public static void clearMarshals(){
		marshals = new HashSet<>();
	}
	
	private Marshal() {
	}

	private Marshal(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	/**
	 * 重写 hashCode 和equals 防重复
	 */
	@Override
	public boolean equals(Object obj) {
		Marshal marshal = (Marshal)obj;
		return this.name.equals(marshal.getName());
	}
	@Override
	public int hashCode() {
		return this.getName().hashCode();
	}

	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return name;
	}
}
